package jp.co.piratealliance.pirateAllianceWeb.web.action.order;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.co.piratealliance.pirateAllianceWeb.common.base.BaseAction;
import jp.co.piratealliance.pirateAllianceWeb.def.Constants;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.ResultDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.EvaluationInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.MessageInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.OrderHisInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.OrderInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.helper.CommonHelper;
import jp.co.piratealliance.pirateAllianceWeb.service.order.OrderService;


/**
 * copyright pirate alliance team 2014
 *
 * @author lunxing
 */

public class OrderAction extends BaseAction {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private OrderService orderService;

	private ResultDto resultDto;

	private OrderInfoDto orderInfoDto;

	private MessageInfoDto messageInfoDto;

	private String user_id;

	private String goods_id;

	private String language_id;

	private List<OrderHisInfoDto> orderHisInfoList;

	private String status;

	private List<EvaluationInfoDto> evaluationInfoList;

	private Map<String, Integer> averageEvaluation;

	public String getOrderHisInfo()
	{
		this.orderHisInfoList = orderService.selectOrderHis(user_id, status);
		return SUCCESS;
	}

	public String getOrdersByUserIdAndGoodsIdOne()
	{
		this.orderInfoDto = this.orderService.getOrdersByUserIdAndGoodsIdOne(this.user_id, this.goods_id, this.language_id);
		return SUCCESS;
	}

	public String insertOrder()
	{
		String orderId = CommonHelper.getSequence(Constants.TB_ORDERS);

		this.orderInfoDto.setOrder_id(orderId);
		this.orderInfoDto.setOrder_type_id(0);
		this.orderInfoDto.setTotal_price(0f);
		this.orderInfoDto.setStatus(0);
		this.orderInfoDto.setCost_plan(0f);
		this.orderInfoDto.setService_price(0f);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strNowTime = df.format(new Date());
		this.orderInfoDto.setOrder_time(strNowTime);
		this.orderInfoDto.setUpdate_time(strNowTime);

		int count = this.orderService.insertOrder(this.orderInfoDto);

		if (1 != count) {
			this.orderInfoDto = null;
		}
		return SUCCESS;
	}

	public String updateOrder()
	{
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strNowTime = df.format(new Date());
		this.orderInfoDto.setUpdate_time(strNowTime);

		int count = this.orderService.updateOrder(this.orderInfoDto);
		if (1 != count) {
			//插入失败,Id设置为空
			this.orderInfoDto = null;
		}
		return SUCCESS;
	}

	/**
	 * 支付
	 * @return
	 */
	public String pay()
	{
		OrderInfoDto orderInfoUptDto = new OrderInfoDto();
		orderInfoUptDto.setOrder_id(this.orderInfoDto.getOrder_id());
		orderInfoUptDto.setUser_id(this.orderInfoDto.getUser_id());
		orderInfoUptDto.setStatus(Constants.ORDER_STATUS_PAY);

		int cnt = this.orderService.updateOrder(orderInfoUptDto, this.messageInfoDto);

		this.resultDto = new ResultDto();
		if (1 == cnt) {
			this.resultDto.setResult(true);
		} else {
			this.resultDto.setResult(false);
		}

		return SUCCESS;
	}

	/**
	 * 验收
	 * @return
	 */
	public String agree()
	{
		OrderInfoDto orderInfoUptDto = new OrderInfoDto();
		orderInfoUptDto.setOrder_id(this.orderInfoDto.getOrder_id());
		orderInfoUptDto.setUser_id(this.orderInfoDto.getUser_id());
		orderInfoUptDto.setStatus(Constants.ORDER_STATUS_OK);

		if(this.messageInfoDto != null)
		{
			this.messageInfoDto.setMessage_id(CommonHelper.getSequence(Constants.TB_MESSAGE));
			this.messageInfoDto.setOrder_id(this.orderInfoDto.getOrder_id());
			this.messageInfoDto.setUser_id(this.orderInfoDto.getUser_id());
			this.messageInfoDto.setStatus(String.valueOf(Constants.STATUS_ACTIVE));
			//this.messageInfoDto.setMessage_type_id("");
		}

		int cnt = this.orderService.updateOrder(orderInfoUptDto, this.messageInfoDto);

		this.resultDto = new ResultDto();
		if (1 == cnt) {
			this.resultDto.setResult(true);
		} else {
			this.resultDto.setResult(false);
		}

		return SUCCESS;
	}

	/**
	 * 拒绝
	 * @return
	 */
	public String refuse()
	{
		OrderInfoDto orderInfoUptDto = new OrderInfoDto();
		orderInfoUptDto.setOrder_id(this.orderInfoDto.getOrder_id());
		orderInfoUptDto.setUser_id(this.orderInfoDto.getUser_id());
		orderInfoUptDto.setStatus(Constants.ORDER_STATUS_NG);

		if(this.messageInfoDto != null)
		{
			this.messageInfoDto.setMessage_id(CommonHelper.getSequence(Constants.TB_MESSAGE));
			this.messageInfoDto.setOrder_id(this.orderInfoDto.getOrder_id());
			this.messageInfoDto.setMessage_type_id("");
		}

		int cnt = this.orderService.updateOrder(orderInfoUptDto, this.messageInfoDto);

		this.resultDto = new ResultDto();
		if (1 == cnt) {
			this.resultDto.setResult(true);
		} else {
			this.resultDto.setResult(false);
		}

		return SUCCESS;
	}

	/*取得评价信息*/
	public String getEvaluationInfoDtoList(){
		this.evaluationInfoList = this.orderService.getEvaluationInfoListByGoodsId(goods_id);
		return SUCCESS;
	}

	public String getAverageEvaluationInfo(){
		Integer value =  this.orderService.getAverageEvaluation(goods_id);
		this.averageEvaluation = new HashMap<String,Integer>();
		this.averageEvaluation.put("averageEvaluation", value == null? 0:value);
		return SUCCESS;
	}

	/**
	 * @return the orderService
	 */
	public OrderService getOrderService() {
		return orderService;
	}

	/**
	 * @param orderService the orderService to set
	 */
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	/**
	 * @return the orderInfoDto
	 */
	public OrderInfoDto getOrderInfoDto() {
		return orderInfoDto;
	}

	/**
	 * @param orderInfoDto the orderInfoDto to set
	 */
	public void setOrderInfoDto(OrderInfoDto orderInfoDto) {
		this.orderInfoDto = orderInfoDto;
	}

	public MessageInfoDto getMessageInfoDto()
	{
		return messageInfoDto;
	}

	public void setMessageInfoDto(MessageInfoDto messageInfoDto)
	{
		this.messageInfoDto = messageInfoDto;
	}

	public ResultDto getResultDto()
	{
		return resultDto;
	}

	public void setResultDto(ResultDto resultDto)
	{
		this.resultDto = resultDto;
	}

	/**
	 * @return the user_id
	 */
	public String getUser_id() {
		return user_id;
	}

	/**
	 * @param user_id the user_id to set
	 */
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	/**
	 * @return the goods_id
	 */
	public String getGoods_id() {
		return goods_id;
	}

	/**
	 * @param goods_id the goods_id to set
	 */
	public void setGoods_id(String goods_id) {
		this.goods_id = goods_id;
	}

	/**
	 * @return the language_id
	 */
	public String getLanguage_id() {
		return language_id;
	}

	/**
	 * @param language_id the language_id to set
	 */
	public void setLanguage_id(String language_id) {
		this.language_id = language_id;
	}

	/**
	 * @return the orderHisInfoList
	 */
	public List<OrderHisInfoDto> getOrderHisInfoList()
	{
		return orderHisInfoList;
	}

	/**
	 * @param orderHisInfoList the orderHisInfoList to set
	 */
	public void setOrderHisInfoList(List<OrderHisInfoDto> orderHisInfoList)
	{
		this.orderHisInfoList = orderHisInfoList;
	}

	/**
	 * @return the status
	 */
	public String getStatus()
	{
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(String status)
	{
		this.status = status;
	}

	/**
	 * @return the evaluationInfoList
	 */
	public List<EvaluationInfoDto> getEvaluationInfoList() {
		return evaluationInfoList;
	}

	/**
	 * @param evaluationInfoList the evaluationInfoList to set
	 */
	public void setEvaluationInfoList(List<EvaluationInfoDto> evaluationInfoList) {
		this.evaluationInfoList = evaluationInfoList;
	}

	/**
	 * @return the averageEvaluation
	 */
	public Map<String, Integer> getAverageEvaluation() {
		return averageEvaluation;
	}

	/**
	 * @param averageEvaluation the averageEvaluation to set
	 */
	public void setAverageEvaluation(Map<String, Integer> averageEvaluation) {
		this.averageEvaluation = averageEvaluation;
	}



}
